import axios from "axios";
axios.defaults.baseURL = "http://www.520gongfu.com:9527/api";

function netWork(info) {
  let type = info.type;
  let url = info.url;
  let data = info.data;
  let head = info.head;
  let tokens = window.sessionStorage.getItem('token');
  return axios({
    method: type,
    url: url,
    data: data,
    headers: { "Content-Type": head, Authorization: tokens },
    // 发送请求之前修改data
    transformRequest: [
      function(data) {
        if (this.method == "get") {
          return data;
        } else {
          let ret = "";
          for (let k in data) {
            // 去除空字符串的请求字段
            if (data[k] !== "") {
              if (ret !== "") ret += "&";
              ret += encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
            }
          }
          return ret;
        }
      },
    ],
  });
}
export default netWork;
